package com.activitycode.activity.square.addmsg;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.activitycode.tools.OptionsTool;
import com.activitycode.tools.imagecache.ImageTools;
import com.duostec.zhuyue.pets.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class GridAdapter extends BaseAdapter {
	public Context context;
	public GridAdapter adapter;
	private List<String> list;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;
	private int screenWidth;

	public GridAdapter(Context context, List<String> list,ImageLoader imageLoader,DisplayImageOptions options,int screenWidth) {
		this.list = list;
		this.context = context;
		this.imageLoader = imageLoader;
		this.options = OptionsTool.getWorkOptions(context);
		this.screenWidth = screenWidth;
		adapter = this;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.adapter_sendwork, null);
			viewHolder.imageView = (ImageView) convertView
					.findViewById(R.id.adapter_sendwork_imageView1);			
			convertView.setTag(viewHolder);
		} else {

			viewHolder = (ViewHolder) convertView.getTag();
		}
		int width = (screenWidth-ImageTools.dip2px(context, 20*2+5*3))/4;
		viewHolder.imageView.getLayoutParams().width = width;
		viewHolder.imageView.getLayoutParams().height = width;
		if(list.get(position).equals(context.getResources().getString(R.string.externalimagepath_tag))){
			viewHolder.imageView.setImageBitmap(null);
//			viewHolder.imageView.setImageResource(R.drawable.style_add_picture);
			imageLoader.displayImage("", viewHolder.imageView, options);
		}else{
			String path = "file://"+list.get(position);
			imageLoader.displayImage(path, viewHolder.imageView, options);
		}
		
		return convertView;
	}

	public final static class ViewHolder {
		
		ImageView imageView;
		
	}
}
